package cn.wen233.sunglow.controllers;

import cn.wen233.basic.http.RestResponse;
import cn.wen233.basic.model.ServiceRecord;
import cn.wen233.core.service.ServiceRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author wenjie
 * @date 2022-05-07 14:43
 */
@RestController
@RequestMapping("/service-record")
@RequiredArgsConstructor
public class ServiceRecordApi {

    @Resource
    private ServiceRecordService serviceRecordService;

    @PostMapping
    public RestResponse<?> create(@RequestBody ServiceRecord serviceRecord) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        serviceRecord.setClientHost(request.getRemoteHost());
        serviceRecordService.create(serviceRecord);
        return RestResponse.success();
    }
}
